func topKFrequent(nums []int, k int) []int {
	hashMap := map[int]int{}
	res := make([]int, k)
	for _, i := range nums {
		hashMap[i]++
	}
	h := &IntHeap{}
	heap.Init(h)

	for key, value := range hashMap {
		heap.Push(h, [2]int{key, value})
		if h.Len() > k {
			heap.Pop(h)
		}
	}

	for i:=0; i<k; i++ {
		res[k-i-1] = heap.Pop(h).([2]int)[0]
	}
    return res
}

type IntHeap [][2]int

func (this IntHeap) Len() int {
    return len(this)
}

func (this IntHeap) Less(i, j int) bool {
    return this[i][1] < this[j][1]
}

func (this IntHeap) Swap(i, j int) {
    this[i], this[j] = this[j], this[i]
}

func (this *IntHeap) Push(v interface{}) {
    *this = append(*this, v.([2]int))
}

func (this *IntHeap) Pop() interface{} {
    old := *this
    n := len(old)
    val := old[n-1]
    *this = old[0:n-1]
    return val
}